/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.ieslaferreria.creditsintesis.rmi;

import edu.ieslaferreria.creditsintesis.logicgame.actions.AddRemotePlayerEvent;
import edu.ieslaferreria.creditsintesis.logicgame.actions.RmiEvent;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Rubén
 * Clase Para RMI, encargada de guardar los puentes de los jugadores y del servidor
 */
public class Partida {
    Object id;/*para actualizar en juego tras sintesis*/
	ArrayList<Pont> ponts = new ArrayList();
    private List<String> alias = new ArrayList();
	// Guardamos el puente del servidor
	PontServer serverGame;
    EventRemoteListener  listener;
    private int PanelActivo=0;
    /*panel activo de ese momento....para ambos bandos(los que preguntan y los que contestan*/


    /** Creates a new instance of Partida */
    public Partida(PontServer sGame){
        this.serverGame = sGame;
        this.setListener(new EventRemoteListener() {
            public void action(RmiEvent event)throws RemoteException{
                if(event instanceof AddRemotePlayerEvent){
//                    System.out.println("ServerGamePont" + serverGame.getPontName());
                    serverGame.send(event);
//                }else if(event instanceof EventGoInInteracction){
//                     EventGoInInteracction evt = (EventGoInInteracction) event;
//                    PanelActivo = evt.getKindPanel();/*aki es donde asignamos el panel activo para saber cual se ve y cual hay que ocultar*/
//                    serverGame.send(evt);
//                }else if(event instanceof EventIniciarInteraccion){
//                     EventIniciarInteraccion evt = (EventIniciarInteraccion) event;
//                     serverGame.send(evt);
//                }else if(event instanceof GameEventInteracctionTwoPanels){
//                     GameEventInteracctionTwoPanels evt =  (GameEventInteracctionTwoPanels) event;
//                     serverGame.send(evt);
                }else{
                    onMessage(event);
            }
        }});
    }
    /** ADD client in Partida, the flag is to send it to all objects **/
    public void addClient(Pont p, String alias, boolean flag) throws RemoteException{
        this.ponts.add(p);
        this.getAlias().add(alias);
        if(flag){
//            System.out.println(flag);
            serverGame.actualiza(p);
        }
    }

    private void setListener(EventRemoteListener  e){
        this.listener = e;
    }

    public EventRemoteListener  getListener(){
        return this.listener;
    }

    private void onMessage(RmiEvent mess) throws RemoteException{
        for(int ii=0; ii<this.ponts.size(); ii++){
/*alias del que envia*/if(!mess.getEventGeneratorPontName().equals(ponts.get(ii).getPontName())){
//                System.out.println("Nombre del Server" + this.serverGame.getPontName());
//                System.out.println("Nombre del Puente" + this.ponts.get(ii).getPontName());
                            (this.ponts.get(ii)).send(mess);
                          }
    }
    }

    public int countClients() throws RemoteException{
        return this.getAlias().size();
    }

    public void removeClients(String alias, Pont p) throws RemoteException{
        this.getAlias().remove(alias);
        this.ponts.remove(p);
    }

    public Pont getPontPlayer(int id){
        return this.ponts.get(id);
    }
  
    public Pont getPontPlayer(String name) throws RemoteException{
        return this.ponts.get(this.getAlias().indexOf(name));
    }

    /**
     * @return the alias
     */
    public List<String> getAlias() throws RemoteException{
        return alias;
    }

    /**
     * @return the PanelActivo
     */
    public int getPanelActivo() {
        return PanelActivo;
    }

    /**
     * @param PanelActivo the PanelActivo to set
     */
    public void setPanelActivo(int PanelActivo) {
        this.PanelActivo = PanelActivo;
    }

}



